/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.aaron.platform.view;

import com.aaron.platform.dao.PageInfo;
import com.aaron.platform.entity.GczxOrderInfo;
import com.aaron.platform.service.IDomainService;
import com.aaron.platform.service.IEnterpriseMgr;
import com.aaron.platform.service.IOrderMgr;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import javax.faces.model.ListDataModel;
import javax.faces.model.SelectItem;
import org.apache.log4j.Logger;

/**
 *
 * @author mac
 */
public class OrderInputBean extends DataListBean {

    private static Logger logger = Logger.getLogger(OrderInputBean.class);

    private IOrderMgr orderMgr;

    private int orderPro;

    private List<SelectItem> orderProList;

    /** 选择的区域 */
    private int domainId;

    /** 对应的单位 */
    private int enterpriseId;

    /** 选中查看明细的订单信息 */
    private GczxOrderInfo selectedOrderInfo;

    public void setOrderMgr(IOrderMgr orderMgr) {
        this.orderMgr = orderMgr;
    }

    public OrderInputBean() {
        super();
        this.tableTitle = FacesUtil.getString("table_title_orderlist");

        // 删除、编辑、查看操作
        this.deletable = false;
        this.editable = true;
        this.viewable = false;
        multiSelectable = false;

        pagable = true;
        selectedOrderInfo = new GczxOrderInfo();
        pageInfo = new PageInfo<GczxOrderInfo>();
    }

    @Override
    public void queryData() {

        // 设置查询条件
        Properties properties = new Properties();

        // 设置订单编号为空
        properties.setProperty(GczxOrderInfo.ORDER_STATUS, String.valueOf(0));

        if (this.orderPro != 0) {
            properties.setProperty(GczxOrderInfo.ORDER_PRO, String.valueOf(this.orderPro));
        }
       
        pageInfo = orderMgr.findByCondition(properties, pageInfo.getStartIndex(), pageInfo.getPageSize());
        logger.info("Query order(status:0) list finished.");
        data.setWrappedData(pageInfo.getItems());
    }

    @Override
    public void initColumnHeaders() {
        headerColumns.add(new ColumnType(ColumnType.COLUMN_TEXT, "project_code",
                FacesUtil.getString("table_column_projectcode"), false, "6%"));
        headerColumns.add(new ColumnType(ColumnType.COLUMN_TEXT, "contact_code",
                FacesUtil.getString("table_column_contactcode"), false, "15%"));
        headerColumns.add(new ColumnType(ColumnType.COLUMN_TEXT, "enterprise_name",
                FacesUtil.getString("table_column_entprisename"), false, "15%"));
        headerColumns.add(new ColumnType(ColumnType.COLUMN_TEXT, "task_name",
                FacesUtil.getString("table_column_taskname"), false, "15%"));
        headerColumns.add(new ColumnType(ColumnType.COLUMN_TEXT, "order_value",
                FacesUtil.getString("table_column_ordervalue"), false, "8%"));
        headerColumns.add(new ColumnType(ColumnType.COLUMN_TEXT, "order_desc",
                FacesUtil.getString("table_column_orderdesc"), false, "15%"));
    }

    @Override
    public String getColumnValue() {
        String columnName = getColumnName();
        increaseColumnIndex();
        
        GczxOrderInfo obj = (GczxOrderInfo) data.getRowData();
        if (columnName.equals("project_code")) {
             return obj.getProjectCode();
        } else if (columnName.equals("contact_code")) {
            return obj.getContactCode();
        } else if (columnName.equals("enterprise_name")) {
            return obj.getEnterpriseInfo().getEnterpriseName();
        } else if (columnName.equals("task_name")) {
            return obj.getTaskName();
        } else if (columnName.equals("order_value")) {
            return String.valueOf(obj.getOrderValue());
        } else if (columnName.equals("order_desc")) {
            return obj.getOrderDesc();
        }
        else {
            return "";
        }
    }

    /**
     * 初始化查询条件
     */
    public void reset()
    {
        this.orderPro = 0;
        this.data = new ListDataModel();
        pageInfo = new PageInfo<GczxOrderInfo>();
    }

    /** 获取专业列表 */
    public List<SelectItem> getOrderProList() {
        if (this.orderProList == null) {
            orderProList = new ArrayList<SelectItem>();
            orderProList.add(new SelectItem(0, FacesUtil.getString("list_item_default")));
            orderProList.add(new SelectItem(1, FacesUtil.getString("list_item_jz")));
            orderProList.add(new SelectItem(2, FacesUtil.getString("list_item_cs")));
            orderProList.add(new SelectItem(3, FacesUtil.getString("list_item_fgys")));
            orderProList.add(new SelectItem(4, FacesUtil.getString("list_item_qyw")));
        }
        return orderProList;
    }

    /**
     * 编辑数据：修改订单信息
     * @return 返回页面导航
     */
    @Override
    public String editData()
    {
        this.selectedOrderInfo = (GczxOrderInfo) data.getRowData();
        this.domainId = selectedOrderInfo.getDomainInfo().getDomainId();
        this.enterpriseId = selectedOrderInfo.getEnterpriseInfo().getEnterpriseId();
        return "order_input_detail";
    }

    public String inputOrderCode() {

        // 设置执行状态
        selectedOrderInfo.setOrderStatus(1);
        orderMgr.updateOrderInfo(selectedOrderInfo);

        FacesUtil.addInfoMessage(null, "info_update_order_success", selectedOrderInfo.getOrderCode());

        queryData();
        return "order_input_list";
    }

    public String toPriviousPage() {
        return "order_input_list";
    }

    public static Logger getLogger() {
        return logger;
    }

    public static void setLogger(Logger logger) {
        OrderInputBean.logger = logger;
    }

    public int getOrderPro() {
        return orderPro;
    }

    public void setOrderPro(int orderPro) {
        this.orderPro = orderPro;
    }

    public GczxOrderInfo getSelectedOrderInfo() {
        return selectedOrderInfo;
    }

    public int getDomainId() {
        return domainId;
    }

    public void setDomainId(int domainId) {
        this.domainId = domainId;
    }

    public int getEnterpriseId() {
        return enterpriseId;
    }

    public void setEnterpriseId(int enterpriseId) {
        this.enterpriseId = enterpriseId;
    }
}
